﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Runtime.Serialization;
using System.Web;

namespace AssetService.Model
{
    [DataContract (IsReference=true)]
    public class User : Utilities
    {
        // Properties
        private int _id = 0;
        private string _username;
        private string _password;

        // gets user id
        [DataMember]
        public int UserID
        {
            get { return _id; }
            set { _id = value; }
        }

        // get or set username
        [DataMember]
        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

        // get or set password
        [DataMember]
        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        //public User()
        //{
        //    _id = 0;
        //}

        //public User(int userid)
        //{
        //    if (userid > 0)
        //    {
        //        _id = userid;

        //    }
        //}
        

        public User(string Username, string Password)
        {
            // Execute login stored procedure to validate user
            SqlParameter[] parameters = new SqlParameter[] {
                new SqlParameter("@Username", Username),
                new SqlParameter("@Password", Password)
            };
            DataTable dt = ReadData("spLogin", parameters);
            if (dt.Rows.Count > 0)
            {
                foreach(DataRow r in dt.Rows) 
                {
                    UserID = Convert.ToInt32(r["pkUserID"].ToString());
                    this.Username = r["Username"].ToString();
                    this.Password = r["Password"].ToString();
                }
            }
            else
                _id = 0;
        }

        //public bool RegisterUser(string Username, string Password)
        //{
        //    try
        //    {
        //        // Execute register stored procedure to create new user
        //        SqlParameter[] parameters = new SqlParameter[] {
        //            new SqlParameter("@Username", Username),
        //            new SqlParameter("@Password", Password)
        //        };
        //        ExecuteProcedure("spRegisterUser", parameters);
        //        return true;
        //    }
        //    catch
        //    {
        //        return false;
        //    }

        //}
    }
}